﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Indigo.Quibbler.Data;
using System.Windows.Threading;

namespace Indigo.Quibbler.Controls
{
    /// <summary>
    /// Interaction logic for PhotoGalleryControl.xaml
    /// </summary>
    public partial class PhotoGalleryControl : UserControl
    {

        #region Properties
        private int _currPhotoIndex = 0;
        private DispatcherTimer _timer = new DispatcherTimer();
        public List<NewsArticle> ItemSource
        {
            get { return (List<NewsArticle>)GetValue(ItemSourceProperty); }
            set
            {
                SetValue(ItemSourceProperty, value);
            }
        }
        public int SelectedIndex
        {
            get { return (int)GetValue(SelectedIndexProperty); }
            set
            {
                SetValue(SelectedIndexProperty, value);
            }
        }
        public bool ShowClose
        {
            get { return (bool)GetValue(ShowCloseProperty); }
            set { SetValue(ShowCloseProperty, value); }
        }
        #endregion

        #region Dependency Property
        public static readonly DependencyProperty ShowCloseProperty =
            DependencyProperty.Register("ShowClose", typeof(bool), typeof(PhotoGalleryControl), new UIPropertyMetadata(false, OnShowCloseChanged));
        public static readonly DependencyProperty ItemSourceProperty = DependencyProperty.Register("ItemSource", typeof(List<NewsArticle>), typeof(PhotoGalleryControl), new PropertyMetadata(OnItemSourceChanged));
        public static readonly DependencyProperty SelectedIndexProperty = DependencyProperty.Register("SelectedIndex", typeof(int), typeof(PhotoGalleryControl), new PropertyMetadata(0, OnSelectedIndexChanged));
        #endregion

        #region Event
        public delegate void PhotoSelectionChangedEventHandler(object sender, int index);
        public event EventHandler Close;
        public event PhotoSelectionChangedEventHandler SelectionChanged;
        #endregion

        #region Ctor
        public PhotoGalleryControl()
        {
            this.InitializeComponent();
            listBox.SelectionChanged += new SelectionChangedEventHandler(listBox_SelectionChanged);
            prevButton.Click += new RoutedEventHandler(prevButton_Click);
            nextButton.Click += new RoutedEventHandler(nextButton_Click);
            _timer.Tick += new EventHandler(_timer_Tick);
            playButton.Click += new EventHandler(playButton_Click);
        }

        void playButton_Click(object sender, EventArgs e)
        {
            if (_timer.IsEnabled == false)
            {
                _timer.IsEnabled = true;
                playButton.ImageSource = playButton.HoverImageSource = "pack://application:,,/Quibbler - Universal News Reader;component/Images/pause_button.png";
            }
            else
            {
                _timer.IsEnabled = false;
                playButton.ImageSource = playButton.HoverImageSource = "pack://application:,,/Quibbler - Universal News Reader;component/Images/play_button.png";
            }
        }

        void _timer_Tick(object sender, EventArgs e)
        {
            if (this.Visibility == Visibility.Visible)
                nextButton_Click(null, null);
            else //Stop the timer if Photogallery is Collapsed
                _timer.IsEnabled = false;
        }


        void nextButton_Click(object sender, RoutedEventArgs e)
        {
            if (nextButton.IsEnabled == false) return;
            _currPhotoIndex++;
            mainImage.Source = ItemSource[listBox.SelectedIndex].Images[_currPhotoIndex].Url;
            txtSubHeadline.Text = ItemSource[listBox.SelectedIndex].Images[_currPhotoIndex].Caption;
            if (ItemSource[listBox.SelectedIndex].Images.Count - 1 == _currPhotoIndex)
            {
                nextButton.IsEnabled = false;
                playButton.ImageSource = playButton.HoverImageSource = "pack://application:,,/Quibbler - Universal News Reader;component/Images/play_button.png";
                _timer.IsEnabled = false;
            }
            if (_currPhotoIndex > 0 && prevButton.IsEnabled == false)
                prevButton.IsEnabled = true;
        }

        void prevButton_Click(object sender, RoutedEventArgs e)
        {
            _currPhotoIndex--;
            mainImage.Source = ItemSource[listBox.SelectedIndex].Images[_currPhotoIndex].Url;
            txtSubHeadline.Text = ItemSource[listBox.SelectedIndex].Images[_currPhotoIndex].Caption;
            if (_currPhotoIndex == 0)
                prevButton.IsEnabled = false;
            if (ItemSource[listBox.SelectedIndex].Images.Count - 1 > _currPhotoIndex && nextButton.IsEnabled == false)
                nextButton.IsEnabled = true;
        }
        #endregion

        #region Handlers
        void listBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                mainImage.Source = ItemSource[listBox.SelectedIndex].Images[0].Url;
                txtHeadline.Text = ItemSource[listBox.SelectedIndex].Headline;
                if (!String.IsNullOrEmpty(ItemSource[listBox.SelectedIndex].SubHeadline) && (ItemSource[listBox.SelectedIndex].Headline != ItemSource[listBox.SelectedIndex].SubHeadline))
                    txtHeadline.Text += " : " + ItemSource[listBox.SelectedIndex].SubHeadline;
                txtSubHeadline.Text = ItemSource[listBox.SelectedIndex].Images[0].Caption;
                _currPhotoIndex = 0;
                _timer.Interval = new TimeSpan(0, 0, 5);
                _timer.IsEnabled = false;
                playButton.ImageSource = playButton.HoverImageSource = "pack://application:,,/Quibbler - Universal News Reader;component/Images/play_button.png";
                prevButton.IsEnabled = false;
                if (ItemSource[listBox.SelectedIndex].Images.Count - 1 > 0)
                    nextButton.IsEnabled = true;
                else
                    nextButton.IsEnabled = false;
                this.SelectedIndex = listBox.SelectedIndex;
                listBox.ScrollIntoView(listBox.SelectedItem);
                if (SelectionChanged != null)
                    SelectionChanged(this, listBox.SelectedIndex);
            }
            catch
            {
                mainImage.Source = string.Empty;
                txtHeadline.Text = string.Empty;
                txtSubHeadline.Text = string.Empty;
                this.SelectedIndex = listBox.SelectedIndex;
            }
        }

        private static void OnItemSourceChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
        {
            (obj as PhotoGalleryControl).OnItemSourceChanged(e);
        }

        private static void OnSelectedIndexChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
        {
            (obj as PhotoGalleryControl).OnSelectedIndexChanged(e);
        }

        private void OnSelectedIndexChanged(DependencyPropertyChangedEventArgs e)
        {
            try
            {
                if ((int)e.NewValue >= 0 && (int)e.NewValue <= ItemSource.Count - 1)
                    listBox.SelectedIndex = (int)e.NewValue;
                else
                    listBox.SelectedIndex = 0;

            }
            catch { }
        }

        private static void OnShowCloseChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
        {
            (obj as PhotoGalleryControl).OnShowCloseChanged(e);
        }

        private void OnShowCloseChanged(DependencyPropertyChangedEventArgs e)
        {
            try
            {
                if ((bool)e.NewValue == true)
                    closePhoto.Visibility = Visibility.Visible;
                else
                    closePhoto.Visibility = Visibility.Collapsed;
            }
            catch { }
        }

        private void OnItemSourceChanged(DependencyPropertyChangedEventArgs e)
        {
            try
            {

                // var cur = from x in ItemSource select x.ImageUrl;

                listBox.ItemsSource = ItemSource;
                if (SelectedIndex >= 0 && SelectedIndex <= ItemSource.Count - 1)
                    listBox.SelectedIndex = SelectedIndex;
                else
                    listBox.SelectedIndex = 0;
            }
            catch { }
        }

        private void btnPhotoClose_Click(object sender, RoutedEventArgs e)
        {
            if (Close != null)
                Close(sender, e);
        }

        void _timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            nextButton_Click(null, null);
        }

        #endregion
    }
}
